package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisMessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.utils.SMSUtils;
import cn.itcast.utils.ValidateCodeUtils;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/*
 *Author:TH
 *Dete:2019/10/2711:47
 */
@RestController
@RequestMapping("/validate")
public class ValidateController {

    @Autowired
    private JedisPool jedisPool;

    //预约
    @RequestMapping("/sendcode")
    public Result sendCode(String telephone){
        //生成验证码

        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        System.out.println("************************");
        System.out.println("用户手机号为:"+telephone);
        System.out.println("用户验证码为:"+code);


        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);
            //发送成功,将验证码存入redis数据库,5分钟有效
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            //发送失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    //快速登录
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //生成验证码

        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        System.out.println("************************");
        System.out.println("用户手机号为:"+telephone);
        System.out.println("用户验证码为:"+code);

        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);
            //发送成功,将验证码存入redis数据库,5分钟有效
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,300,code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            //发送失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
